﻿<%@page contentType="text/html;charset=utf-8" import="
		java.util.List,java.util.Iterator,java.math.BigDecimal,java.util.Properties,
		java.util.ArrayList,java.util.HashMap,java.util.Map,java.util.StringTokenizer,
		org.json.simple.JSONObject,org.json.simple.JSONArray,com.innoace.mo.sql.*,
		com.innoace.mwp.MwpObject,com.innoace.mwp.MwpArray,com.innoace.mwp.MwpContext,
		com.innoace.mo.MoAction,com.innoace.mo.MoException,com.innoace.mo.MoFormat,
		org.apache.log4j.Logger,java.io.PrintWriter,sun.misc.*,java.net.*,java.io.*,
		com.innoace.mwp.*,java.util.*,java.text.*,org.apache.http.*,java.sql.*,org.json.simple.parser.*,com.innoace.mwp.MwpPlatform,com.innoace.mo.iss.*, com.innoace.mo.sql.vote.*
	"
%><%!
	public String[] canVote(MwpContext ctx, HashMap<String, String> hmParam){
		String canVote = "";
		String applyRate = "0";
		String startDate = "";
		String endDate = "";
		String sVoteInfo[] = {"","0", "", ""};
		try{			
			String sVoteSeq = hmParam.get("voteSeq");
			String sSwbeonho = hmParam.get("swbeonho");	

			java.sql.Connection con = ctx.con;						

			Vote vote = new Vote();
			vote.voteSeq = sVoteSeq;
			vote.mdn = sSwbeonho;			

			List<Vote> list = ctx.mapper(VoteMapperOracle.class).canVote(vote);

			if(list != null && list.size() > 0){
				canVote = list.get(0).voteYn;
				applyRate = list.get(0).applyRate;
				startDate = list.get(0).startDate;
				endDate = list.get(0).endDate;
			}else{
				canVote = "N";
			}

			System.out.println("canVote[canVote] : " +canVote);			
			System.out.println("canVote[applyRate] : " +applyRate);			
			System.out.println("canVote[startDate] : " +startDate);			
			System.out.println("canVote[endDate] : " +endDate);			

			sVoteInfo[0] = canVote;
			sVoteInfo[1] = applyRate;
			sVoteInfo[2] = startDate;
			sVoteInfo[3] = endDate;
					
			con.commit();
			con.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			return sVoteInfo;		
		}
	}	
%>

<%
	String id = request.getParameter("id");	
	boolean canVote = false;
	String alertMsg = "";
	String[] voteInfo = null;

	String sVoteYn = "";
	String sStartDate = "";
	String sEndDate = "";

	MwpContext ctx = MwpContext.getContext();		
	com.innoace.mo.MoFormat fmt = com.innoace.mo.MoFormat.getFormat();
	try{		
		ctx.init(request, response, out);
		
		HashMap<String, String> hmParam = new HashMap<String, String>(); 
		hmParam.put("voteSeq", "4");
		hmParam.put("swbeonho", id);						
		
		voteInfo = canVote(ctx, hmParam);
		
		if(voteInfo != null && voteInfo.length >= 4){
			sVoteYn = voteInfo[0];
			sStartDate = voteInfo[2];				
			sEndDate = voteInfo[3];				
		}else{
			sVoteYn = "";
			sStartDate = "";					
			sEndDate = "";					
		}		
	}finally{
		ctx.destroy();
		
		if(sVoteYn.equals("E")){
			//투표했음
			alertMsg = "이미 투표하셨습니다.";													
		}else{
			//투표종료
			alertMsg = "투표 가능 시간이 아닙니다.\n"+sStartDate+" 부터 가능합니다.";
		}					
		
	}	
	
	if(sVoteYn != null && !sVoteYn.equals("") && sVoteYn.equals("Y")){
		out.println("success");
	}else{
		out.println(alertMsg);
	}
%>